/*
 * @Author: wayne cao 
 * @Date: 2022-05-04 10:21:43 
 * @Last Modified by: wayne cao
 * @Last Modified time: 2022-05-25 09:05:42
 */
#include "qs_config.h"
#include "qs_log.h"
#include "qs_sched.h"
#include "py32f002b_it.h"
#include "qs_analog.h"
#include "GXHT3X.h"
#include "qs_action.h"
#include "qs_settings.h"
#include "qs_crc8.h"
static qs_rf_protocol_on_1ms_handle_s m_rf_protocol_on_1ms_handle = NULL;
static qs_on_action_20ms_handle_s m_on_action_20ms_handle = NULL;
static qs_on_action_critical_1s_handle_s m_on_critical_action_1s_handle = NULL;
static qs_on_keys_20ms_handle_s m_on_keys_20ms_handle = NULL;
static qs_on_misc_20ms_handle_s m_on_misc_20ms_handle = NULL;
static qs_on_job_20ms_handle_s m_on_job_20ms_handle = NULL;
static qs_on_ble_protocol_10ms_handle_s m_on_ble_protocol_10ms_handle = NULL;
static volatile uint8_t flag_1ms = 0; 
static uint64_t jiffies = 0;

static void on_1ms_timeout_timer(void)
{
    static uint16_t prescale = 1000;
    static uint16_t prescale_10ms = 10;
    jiffies ++;
    if(flag_1ms < UINT8_MAX) {
        flag_1ms ++;
    }
    prescale_10ms --;
    if(prescale_10ms == 0) {
        prescale_10ms = 10;
        if(m_on_ble_protocol_10ms_handle) {
            m_on_ble_protocol_10ms_handle();
        }
    }
    
    
    prescale --;
    if(prescale == 0) {
        prescale = 1000;
        if(m_on_critical_action_1s_handle) {
            m_on_critical_action_1s_handle();
        }
    }
}

uint64_t get_sys_runtime_ms(void)
{
    return jiffies;
}
void runtime_print_task(void *p_data, uint16_t data_len)
{
//  static  uint8_t rom_data[8] = {0x79, 0x06, 0x01, 0xEF, 0xFF, 0x50, 0xAF, 0xD5};
//  uint8_t crc_cal = CRC8Calculate(rom_data, 8);
//  QS_LOG_INFO(">> %lld, crc %x \r\n",  get_sys_runtime_ms()/1000, crc_cal);
//  
  qs_settings_job_t* p_settings_job = qs_settings_job_get();
  qs_settings_sens_t* p_settings_sens = qs_settings_sens_get();
//  QS_LOG_INFO(">> %lld, mode [%d,%d] fan [%d], heat [%d,%.4f] timer %d, t %d, th0 [%d,%d],  th1 [%d,%d]\r\n",
//                get_sys_runtime_ms()/1000, p_settings_job->work_mode, p_settings_job->auto_mode, p_settings_job->fan_level, p_settings_job->heat_level, qs_action_heat_voltage_read(),
//                p_settings_job->timer, p_settings_sens->ntc_temp, p_settings_sens->temp0, p_settings_sens->humid0,p_settings_sens->temp1, p_settings_sens->humid1);
//  QS_LOG_INFO("heat [%d,%.4f]\r\n", p_settings_job->heat_level, qs_action_heat_voltage_read());
//  QS_LOG_INFO(">> %lld,timer %d\r\n", get_sys_runtime_ms()/1000, p_settings_job->timer);

}

void qs_sched_processing(void)
{
    static uint16_t prescale_1s = 1000;
    static uint16_t prescale_5ms = 5;
    static uint8_t step = 0;
    if(flag_1ms > 0) {
        LL_IWDG_ReloadCounter(IWDG);
        flag_1ms = 0;
        
        if(m_rf_protocol_on_1ms_handle) {
            m_rf_protocol_on_1ms_handle();
        }
        
        prescale_5ms --;
        if(prescale_5ms == 0) {
            prescale_5ms = 5;
            switch(step) {
            case 0:
                if(m_on_action_20ms_handle) {
                    m_on_action_20ms_handle();
                }
                break;
            case 1:
                if(m_on_keys_20ms_handle) {
                    m_on_keys_20ms_handle();
                }
                break;
            case 2:
                if(m_on_misc_20ms_handle) {
                    m_on_misc_20ms_handle();
                }
                if(m_on_job_20ms_handle) {
                    m_on_job_20ms_handle();
                };
                break;
            default:;
              
            }
            step ++;
            step &= 0x03;
        }
        
        prescale_1s --;
        if(prescale_1s == 0) {
            prescale_1s = 1000;
            runtime_print_task(NULL, 0);
        }
    }
    
    
}


qs_ret_code_t qs_sched_init(void)
{
    qs_sched_on_1ms_timeout_timer_evt_set(on_1ms_timeout_timer);
    return QS_SUCCESS;
}

void qs_rf_protocol_on_1ms_handle_set(qs_rf_protocol_on_1ms_handle_s on_1ms_handle)
{
    m_rf_protocol_on_1ms_handle = on_1ms_handle;
}
void qs_on_action_20ms_handle_set(qs_on_action_20ms_handle_s on_action_20ms_handle)
{
    m_on_action_20ms_handle = on_action_20ms_handle;
}
void qs_on_action_critical_1s_handle_set(qs_on_action_critical_1s_handle_s on_critical_action_1s_handle)
{
    m_on_critical_action_1s_handle = on_critical_action_1s_handle;
}
void qs_on_keys_20ms_handle_set(qs_on_keys_20ms_handle_s on_keys_20ms_handle)
{
    m_on_keys_20ms_handle = on_keys_20ms_handle;
}
void qs_on_misc_20ms_handle_set(qs_on_misc_20ms_handle_s on_misc_20ms_handle)
{
    m_on_misc_20ms_handle = on_misc_20ms_handle;
}
void qs_on_job_20ms_handle_set(qs_on_job_20ms_handle_s on_job_20ms_handle)
{
    m_on_job_20ms_handle = on_job_20ms_handle;
}
void qs_on_ble_protocol_10ms_handle_set(qs_on_ble_protocol_10ms_handle_s on_ble_protocol_10ms_handle)
{
    m_on_ble_protocol_10ms_handle = on_ble_protocol_10ms_handle;
}